<template>
  <div class="shop">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card mt-15">
          <div class="home-form">
            <el-form ref="ruleForm" label-width="80px" @submit.native.prevent>
              <el-row :gutter="10">
                <el-col :span="4">
                  <el-form-item label="订单状态" prop="status">
                    <el-select v-model="status" placeholder="请选择" style="width:100%" clearable>
                      <el-option
                        v-for="item in statusArr"
                        :key="item.value"
                        :label="item.name"
                        :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="2">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch" class="search-btn">搜索</el-button>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
        <el-card class="box-card">
          <!-- 头部 -->
          <div slot="header" class="clearfix fix-lh">
            <span>订单管理</span>
          </div>
          <div class="home-body">
            <cvue-table
              :option="tableOption"
              :data="tableData"
              ref="tbl1"
              :isShowAdd="false"
              :page="page"
              :tablePage="tablePage"
              :pageSize="page.pageSize"
              :loading="tableLoading"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              :isShowPage="true">
              <template slot-scope="scope" slot="status">
                <span>{{ setStatus(scope.row.status) }}</span>
              </template>
                <template slot-scope="scope" slot="refund_type">
                <span>{{ setRefundType(scope.row.refund_type) }}</span>
              </template>
              <!-- 操作按钮 -->
              <el-table-column fixed="right" slot="actionMenu" label="操作" align="center" header-align="center" width="160">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="handleRefund(scope.row)" v-hasPermission="permission.edit" v-if="scope.row.refund_type == 1 && scope.row.status > 10">退款</el-button>
                  <el-button type="text" size="small" @click="handleEdit(scope.row)" v-hasPermission="permission.edit" v-if="scope.row.status == 20 && scope.row.refund_type == 0">发货</el-button>
                  <el-button type="text" size="small" @click="handleDelete(scope.row)" v-hasPermission="permission.delete">删除</el-button>
                </template>
              </el-table-column>
            </cvue-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 编辑弹窗 -->
    <order-edit
      @closeDialog="closeDialog('edit')"
      @confirmDialog="confirmDialog('edit')"
      @handleClose="closeDialog('edit')"
      v-if="dialogOrderEdit"
      :dialogVisible="dialogOrderEdit">
    </order-edit>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import cvueTable from '@/components/cvue-table'
import orderEdit from './orderEdit'
import {getClientHeight} from '@/util/tool'
export default {
  name: 'userIndex',
  components: {
    cvueTable,
    orderEdit
  },
  data () {
    return {
      tablePage: 1,
      tableLoading: false,
      page: {
        total: 1, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
        pageSizes: [10, 50, 100]
      },
      tableOption: {
        stripe: true,
        width: '100%',
        border: true,
        index: false,
        selected: false,
        cloumn: [
          { prop: 'index', label: '', align: 'center', width: 58 },
          { prop: 'id', label: '订单号', align: 'center', sortable: true },
          { prop: 'name', label: '产品名称', align: 'center', sortable: true },
          { prop: 'status', label: '订单状态', align: 'center', sortable: true, slotStatus: true },
          { prop: 'num', label: '数量', align: 'center', sortable: true },
          { prop: 'price_pay', label: '实付', align: 'center', sortable: true },
          { prop: 'refund_type', label: '退款状态', align: 'center', sortable: true, slotStatus: true },
          { prop: 'post_no', label: '物流单号', align: 'center', sortable: true }
        ]
      },
      tableData: [
        // { name: 'ddd', code: 'sss', order: 1, status: 1 }
      ],
      dialogOrderEdit: false,
      actionType: 'add',
      rowData: {},
      status: '',
      selectStatus: '',
      statusArr: [
        {name: '已取消', value: 0},
        {name: '未付款', value: 10},
        {name: '待发货', value: 20},
        {name: '待收货', value: 30},
        {name: '待评价', value: 40},
        {name: '交易完成', value: 50},
        {name: '交易关闭', value: 51}
      ]
    }
  },
  computed: {
    height () {
      var height = getClientHeight() - 170
      return height + 'px'
    },
    isDelete () {
      return this.selectIds.indexOf(this.userInfo.id) > -1
    },
    ...mapGetters(['token', 'userInfo'])
  },
  created () {
    this.handleList(this.selectStatus)
  },
  methods: {
    setStatus (status) {
      if (status == 0) {
        return '已取消'
      } else if (status == 10) {
        return '未付款'
      } else if (status == 20) {
        return '待发货'
      } else if (status == 30) {
        return '待收货'
      } else if (status == 40) {
        return '待评价'
      } else if (status == 50) {
        return '交易完成'
      } else if (status == 51) {
        return '交易关闭'
      } else {
        return '其他'
      }
    },
    setRefundType (type) {
      if (type == 0) {
        return '正常'
      } else if (type == 1) {
        return '申请退款'
      } else if (type == 2) {
        return '已退款'
      }
    },
    // 新增
    handleAdd () {
      this.actionType = 'add'
      this.dialogOrderEdit = true
    },
    // 编辑
    handleEdit (row) {
      this.actionType = 'edit'
      this.dialogOrderEdit = true
      this.rowData = row
    },
    handleDelete (row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id,
          del: 1
        }
        this.$store.dispatch('shop/OrderDelete', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.tablePage = 1
          this.handleList(this.selectStatus)
        }).catch((err) => {
          this.$message({type: 'error', message: err.msg})
        })
      }).catch(() => {
      })
    },
    handleRefund (row) {
      this.$confirm('确定要退款吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
         var params = {
          id: row.id,
          refund_type: 2,
          refund_remark: ''
        }
        this.$store.dispatch('shop/OrderPut', params).then(res => {
          // console.log(res)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.msg
            })
            this.tablePage = 1
            this.handleList(this.selectStatus)
          }
        })
      }).catch(() => {
      })
    },
    handleStatus (item, status) {
      // console.log(item)
      var params = {
        id: item.id,
        status: status
      }
      this.$store.dispatch('shop/OrderStatusPut', params).then(res => {
        // console.log(res)
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.tablePage = 1
          this.handleList(this.selectStatus)
        }
      })
    },
    // 改变pagesize
    handleSizeChange (val) {
      this.page.pageSize = val
      this.handleList(this.selectStatus)
    },
    // 改变页码
    handleCurrentChange (val) {
      this.tablePage = val
      this.handleList(this.selectStatus)
    },
    // 列表接口
    handleList (status) {
      this.tableLoading = true
      var params = {
        page: this.tablePage,
        limit: this.page.pageSize,
        status: status
      }
      this.$store.dispatch('shop/OrderGet', params).then(res => {
        console.log(res)
        var data = res.data.datas
        this.tableData = data
        this.page.total = res.data.count
        this.page.currentPage = this.tablePage
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        this.tableLoading = false
      })
    },
    // 关闭弹窗
    closeDialog (params) {
      if (params == 'edit') {
        this.dialogOrderEdit = false
      }
    },
    // 确认
    confirmDialog (params) {
      if (params == 'edit') {
        this.dialogOrderEdit = false
        this.tablePage = 1
        this.handleList(this.selectStatus)
      }
    },
    handleSearch () {
      this.selectStatus = this.status
      this.tablePage = 1
      this.handleList(this.selectStatus)
    }
  }
}
</script>

<style lang="scss">
.shop {
  padding: 0px !important;
  height: auto !important;
  .btn-list {
    float: right;
    margin-right: 10px;
    font-size: 12px;
    color: #409EFF;
    cursor: pointer;
  }
  .el-card__header {
    border-bottom: none;
  }
  .el-card__header,
  .el-card__body {
    padding: 15px;
  }
  .fix-lh {
    line-height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    color: #606266;
  }
  .left{
    min-height:500px;
    background:#fff;
  }
  .left-tree{
    width: 100%;
    min-height:600px;
    overflow: auto;
  }
  .left-top{
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    color: #333;
    background: #e2e2e2;
    font-size: 15px;
  }
}
</style>
